layui.use(['layer','form'], function(){
    var $ = layui.$, //jQuery
        layer = layui.layer,
      form = layui.form;
  //监听提交
    form.on('submit(login)', function(data){
      return false;
      random($("#captcha_img")); 
    });
  
  //绑定点击事件
    $("#login_submit").on("click",function(){
        var user = $("#user").val(),
          pwd = $("#pwd").val(),
          captcha = $("#captcha").val();
        if(user == '' || user == null){
          layer.msg('账号不能为空', {icon: 5});
          random($("#captcha_img")); 	
        }else if(pwd == '' || pwd == null){
          layer.msg('密码不能为空', {icon: 5});
          random($("#captcha_img")); 
        }else {
          //Ajax提交
            $.ajax({
              url: "php/login.php",
              data: {
                  name: user,
                  password: pwd,
                  captcha:captcha
              },
              type: "post",
              dataType: "text",
              success: function(data) {
                  //接收后台的返回值进行判断
                  if (data == "4") {
                      layer.msg('登录成功', {icon: 6});
                      setInterval(function(){
                          window.location.href = "./biaoge.php";
                      },2000);   
                  } else if (data == "0") {
                      layer.msg('用户不存在', {icon: 5});
                  } else if (data == '6'){
                      layer.msg('验证码错误', {icon: 5});
                  } else if (data == "5") {
                      layer.msg('密码错误', {icon: 5});
                  } else {
                      layer.msg('未知错误', {icon: 5});
                  }
              },
              error: function() {
                  layer.msg('数据请求失败', {icon: 5});
              }
          });
        }
  }); 
  
});